'use strict'

var ENV = 'ONLINE';
const Hapi = require('hapi');//路由
const Inert = require('inert');//文件处理
//const Sequelize = require('sequelize');//操作数据库
const fs = require('fs');//操作数据库

var server = new Hapi.Server();
/*
var sequelize = new Sequelize('wangwang','root','',{
    host: 'localhost',
    dialect: 'mysql',
    timezone:'+08:00'
});

sequelize.authenticate()
    .then(function(err){
        console.log('Sql connection has been established successfully.');
    }).catch(function(err){
        console.log('Unable to connect to the database:', err);
});
*/
server.connection({
  port : 3000
});


if (process.env.NODE_ENV !== 'production') {

  const WebpackConfig = require('./config/webpack.config.js'); // Webpack config
  const HapiWebpackDevMiddleware = require('hapi-webpack-dev-middleware');
  const HapiWebpackHotMiddleware = require('hapi-webpack-hot-middleware');

  server.register([{
    register: HapiWebpackDevMiddleware,
    options: {
      config: WebpackConfig,
      options: {
        noInfo: true,
        publicPath: WebpackConfig.output.publicPath,
        stats: {
          colors: true
        }
      }
    }
  }, {
    register: HapiWebpackHotMiddleware
  }], function (err) {
    if (err) {
      throw err;
    }
  });

}


//server.app.db = sequelize;
//server.app.Sequelize = Sequelize;
server.register([
	Inert,
    require('./main'),
	//require('./routes/wangwang.js'),
    // require('./routes/wantu.js')
],err=>{
	if(err){
		throw err;
	}

    // Start the server
    server.start((err) => {
        console.log('Server running at:', server.info.uri);
    });

});
